package com.kgc.servlet;

import com.alibaba.fastjson.JSONObject;
import com.kgc.entity.Branches;
import com.kgc.service.BranchesService;
import com.kgc.service.impl.BranchesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BranchesServlet extends HttpServlet {

    private BranchesService service = new BranchesServiceImpl();


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");

        String mode = "list";
        String newMode = request.getParameter("mode");
        if(newMode != null && !"".equals(newMode)){
            mode = newMode;
        }

        if("list".equals(mode)){
            //查询列表
            List<Branches> list = service.listBranches();
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();
            String json = JSONObject.toJSONString(list);
            out.print(json);
            out.flush();
            out.close();
        }else if("checkNo".equals(mode)){
            //接受参数
            String businessno = request.getParameter("businessno");
            int flag = service.countBranchesByBusinessNo(businessno);
            Map<String,Object> result = new HashMap<>();
            result.put("flag",flag);
            String json = JSONObject.toJSONString(result);
            PrintWriter out = response.getWriter();
            out.print(json);
            out.flush();
            out.close();
        }else if("insert".equals(mode)){
            //接值
            String name = request.getParameter("name");
            String businessno = request.getParameter("businessno");
            String cityarea = request.getParameter("cityarea");
            String address = request.getParameter("address");
            String telephone = request.getParameter("telephone");
            String summary = request.getParameter("summary");
            //封装对象
            Branches b = new Branches(businessno,name,cityarea,address,telephone,summary);
            int flag = service.insertBranches(b);
            request.setAttribute("flag",flag);
            request.getRequestDispatcher("add.jsp").forward(request,response);
        }else if("findById".equals(mode)){
            //根据id查询对象
            int id = Integer.parseInt(request.getParameter("id"));
            Branches b = service.getBranchesById(id);
            request.setAttribute("b",b);
            request.getRequestDispatcher("show.jsp").forward(request,response);
        }else if("delete".equals(mode)){
            //接受id
            int id = Integer.parseInt(request.getParameter("id"));
            int flag = service.deleteBranchesById(id);
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();
            Map<String,Object> result = new HashMap<>();
            result.put("flag",flag);
            String json = JSONObject.toJSONString(result);
            out.print(json);
            out.flush();
            out.close();
        }

    }
}
